<?php
/*
*
* dtapi 筛选条件生成类
* 项目官网： www.dtapi.cn
* 这是一个完全开源免费的项目，您可以任意使用；我们唯一的要求就是保留本信息，当然您也可以在后面添加自己的版权信息，我们支持任何人使用本软件进行任何商业的非商业用途。
* 如果您有问题或者有更好的想法可以加群 61291160 一起讨论。
* -- 高飞
*
*/
namespace dtapi;
class condition
{
    public static $link; //多组字段之间的连接符
    public static $sql;
    public static $condition;
    public static $debug;

    public function __call($name, $args)
    {
        $name = strtolower($name);
        switch ($name){
            case 'sql':
                self::$sql .= $args[0];
                break;
            case 'and':
                self::$link = 'AND';
                break;
            case 'or':
                self::$link = "OR";
                break;
            case 'return':
                $condition = self::$condition ? self::$condition : '';
                if(self::$sql){
                    $condition .= ' '.self::$sql;
                }
                if(self::$debug) echo self::$condition;
                return $condition;
                break;
            case 'minmax':
                $field = isset($args[0]) ? strip_tags($args[0]) : '';
                if($field){
                    preg_match('/(\[.*\])/',$field,$arr);
                    $safe = isset($arr[0]) && $arr[0] ? $arr[0] : 'number';
                    $field = isset($arr[0]) ? str_replace($safe,'',$field) : $field;
                    $min = isset($args[1]) && $args[1] ? $args[2] : '';
                    $max = isset($args[2]) && $args[2] ? $args[2] : '';
                    if($safe=='time'){
                        $fromtime = $min && is_date($min) ? strtotime($min.' 00:00:00') : $min;
                        $totime = $max && is_date($max) ? strtotime($max.' 23:59:59') : $max;
                        if($fromtime) self::$condition .= (self::$condition ? " AND " : '')."`$field`>=$fromtime";
                        if($totime) self::$condition .= (self::$condition ? " AND " : '')."`$field`<=$totime";
                    }elseif($safe=='date'){
                        $fromdate = $min && is_date($min) ? $min : date('Y-m-d',$min);
                        $todate = $max && is_date($max) ? $max : date('Y-m-d',$max);
                        if($fromdate) self::$condition .= (self::$condition ? " AND " : '')."`$field`>=$fromdate";
                        if($todate) self::$condition .= (self::$condition ? " AND " : '')."`$field`<=$todate";
                    }elseif($safe=='number'){
                        if($min && is_numeric($min)) self::$condition .= (self::$condition ? " AND " : '')."`$field`>=$min";
                        if($max && is_numeric($max)) self::$condition .= (self::$condition ? " AND " : '')."`$field`<=$min";
                    }

                }
                break;
            case 'debug':
                self::$debug = 1;
                break;
            case 'in':
                $field = isset($args[0]) ? strip_tags($args[0]) : '';
                if($field){
                    $value = isset($args[1]) ? strip_tags($args[1]) : '';
                    if($value) self::$sql .=" AND $field IN($value)";
                }
                break;
        }
        return $this;
    }

    /*
     * field('catid[int],title[string],price[float]')
     */

    public static function field($field,$flag='=',$value='',$not=''){
        global $GLOBALS,$AREA,$CAT,$cityid,$DT_URL;
        extract($GLOBALS, EXTR_SKIP);
        if(strlen($field)>2){ //组装sql语句
            $fields = strstr($field,',') ? explode(',',$field) : [$field];
            $condition = '';
            foreach($fields as $k=>$field){
                preg_match('/(\[.*\])/',$field,$arr);
                $safe = isset($arr[0]) ? $arr[0] : '[string]';
                $name = isset($arr[0]) ? str_replace($safe,'',$field) : $field;
                $value = isset($value) && $value && count($fields)==1 ? $value : (isset($$name) && $$name ? $$name : '');
                if($safe=='[int]'){
                    $value = $value ? intval($value) : 0;
                }elseif($safe=='[float]'){
                    $value = $value ? round($value) : '0';
                }else{
                    $value = $value ? htmlspecialchars($value) : '';
                }
                $link = $k==0 ? self::$link : ' AND';
                if(preg_match('/^[a-zA-Z]+$/', $flag)){
                    $flag = strtoupper($flag);
                }
                if(in_array($flag,array('IN','NOT IN'))){
                    $right = " ($value)";
                    $flag = ' '.$flag;
                }else{
                    if(is_numeric($value)){
                        $right = $value;
                    }elseif($value=='null' || $value=='NULL'){
                        $right = "''";
                    }else{
                        $right = "'$value'";
                    }

                }
                if(($value && !$not) || ($value && $not && $value!=$not)){
                    if($flag=='FIND_IN_SET'){
                        $condition .= $link." FIND_IN_SET($right,$name)";
                    }elseif($name=='catid'){ //如果是分类条件，则查询出当前分类下所有子分类的信息
                        $condition .= isset($CAT['child']) && $CAT['child'] ? " AND catid IN (".$CAT['arrchildid'].")" : $link." `$name`".$flag.$right;;
                    }elseif($name=='areaid'){ //如果是地区条件，则查询当前地区下所有子地区的信息
                        if(!$value && $cityid && strpos($DT_URL, 'areaid') === false) {
                            $value = $cityid;
                            $ARE = $AREA[$cityid];
                        }
                        $condition .= isset($ARE['child']) && $ARE['child'] ? " AND areaid IN (".$ARE['arrchildid'].")" : $link." `$name`".$flag.$right;;
                    }else{
                        $condition .= $link." `$name`".$flag.$right;
                    }

                }
            }
            if(self::$link=='OR'){
                $condition = "($condition)";
            }
            self::$condition .= ' '.$condition;
        }
        return new self();
    }


}